package cn.hzking.gjmall.activitys.refund;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.fingdo.statelayout.StateLayout;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.hzking.gjmall.R;
import cn.hzking.gjmall.activitys.order.OrderDetailActivity;
import cn.hzking.gjmall.base.ColorStatuBarActivity;
import cn.hzking.gjmall.bean.refund.OnlyRefundScheduleBean;
import cn.hzking.gjmall.bean.refund.RefundScheduleBean;
import cn.hzking.gjmall.net.RetrofitFactory;
import cn.hzking.gjmall.net.base.BaseEntity;
import cn.hzking.gjmall.net.base.BaseObserver;
import cn.hzking.gjmall.utils.UIUtils;
import cn.hzking.gjmall.view.SimpleOptionView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class RefundScheduleActivity extends ColorStatuBarActivity {
    @Bind(R.id.simpleTileView) SimpleOptionView titleTv;
    @Bind(R.id.sl) StateLayout sl;
    private int orderId;
    private int orderRefundId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_refund_schedule);
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        if(getIntent()!=null){
            orderId=getIntent().getIntExtra("orderId",0);
            orderRefundId=getIntent().getIntExtra("orderRefundId",0);
        }
        init();
    }

    private void init() {
        titleTv.setTitle("退款进度");
        if(orderRefundId==0){
            onlyRefund(orderId);
        }else {
            refundGoods(orderRefundId);
        }
    }

    private void refundGoods(int orderRefundId) {
        RetrofitFactory.getInstant().API()
                .refundSchedule(orderRefundId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RefundScheduleBean>(this,false) {
                    @Override
                    protected void onSuccees(RefundScheduleBean refundScheduleBean) throws Exception {
                        if(refundScheduleBean.getData().getOrderRefundDO()!=null){
                            String refundStatus= refundScheduleBean.getData().getOrderRefundDO().getRefundStatus();
                            String resendCode=refundScheduleBean.getData().getOrderRefundDO().getResendCode();
                            String resendCompany=refundScheduleBean.getData().getOrderRefundDO().getResendCompany();
                            String returnAddress=refundScheduleBean.getData().getReturnAddress();
                            String returnName=refundScheduleBean.getData().getReturnName();
                            String returnTel=refundScheduleBean.getData().getReturnTel();
                            switch (refundStatus){
                                case "WILL_AUDIT"://待审核
                                    waitexam();
                                    break;
                                case "LOGISTICS_AUDIT"://物流审核
                                    if(!TextUtils.isEmpty(resendCode)){
                                        wuliucommited(resendCode,resendCompany);
                                    }else {
                                        if(!TextUtils.isEmpty(returnAddress)){
                                            write(returnAddress,returnName,returnTel);
                                        }
                                    }
                                    break;
                                case "AUDIT_PASS"://审核通过
                                    passexam();
                                    break;
                                case "AUDIT_ON_PASS"://审核不通过
                                    failexam();
                                    break;
                                case "LOGISTICS_NO_PASS"://客服审核不通过
                                    failexam();
                                    break;
                                case "FINISHED_NO_PASS"://财务审核不通过
                                    failexam();
                                    break;
                                case "FINISHED_AUDIT"://已经打款
                                    successrefund();
                                    break;
                                default:
                                    break;
                            }
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        if(isNetWorkError) {
                            UIUtils.showToast(RefundScheduleActivity.this, "网络不给力", Toast.LENGTH_SHORT);
                        }else {
                            UIUtils.showToast(RefundScheduleActivity.this, "服务器错误", Toast.LENGTH_SHORT);
                        }
                    }

                    @Override
                    protected void onCodeError(RefundScheduleBean refundScheduleBean) throws Exception {
                        super.onCodeError(refundScheduleBean);
                        UIUtils.showToastShort(RefundScheduleActivity.this,refundScheduleBean.getErrorMsg());
                    }
                });
    }




    private void onlyRefund(int orderId) {
        RetrofitFactory.getInstant().API()
                .onlyRefundSchedule(orderId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<OnlyRefundScheduleBean>(this,false) {
                    @Override
                    protected void onSuccees(OnlyRefundScheduleBean onlyRefundScheduleBean) throws Exception {
                        if(onlyRefundScheduleBean.getData().getOrderRefundDO()!=null){
                            String refundStatus= onlyRefundScheduleBean.getData().getOrderRefundDO().getRefundStatus();
                            switch (refundStatus){
                                case "WILL_AUDIT"://待审核
                                    waitexam();
                                    break;
                                case "AUDIT_PASS"://审核通过
                                    passexam();
                                    break;
                                case "AUDIT_ON_PASS"://审核不通过
                                    failexam();
                                    break;
                                case "LOGISTICS_NO_PASS"://客服审核不通过
                                    failexam();
                                    break;
                                case "FINISHED_NO_PASS"://财务审核不通过
                                    failexam();
                                    break;
                                case "FINISHED_AUDIT"://已经打款
                                    successrefund();
                                    break;
                                default:
                                    break;
                            }
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        if(isNetWorkError) {
                            UIUtils.showToast(RefundScheduleActivity.this, "网络不给力", Toast.LENGTH_SHORT);
                        }else {
                            UIUtils.showToast(RefundScheduleActivity.this, "服务器错误", Toast.LENGTH_SHORT);
                        }
                    }

                    @Override
                    protected void onCodeError(OnlyRefundScheduleBean onlyRefundScheduleBean) throws Exception {
                        super.onCodeError(onlyRefundScheduleBean);
                        UIUtils.showToastShort(RefundScheduleActivity.this,onlyRefundScheduleBean.getErrorMsg());
                    }
                });
    }

    private void successrefund() {
        View view = LayoutInflater.from(this).inflate(R.layout.layout_refund_agree,null);
        sl.showCustomView(view);
    }

    private void failexam() {
        View view = LayoutInflater.from(this).inflate(R.layout.layout_refund_failexam,null);
        sl.showCustomView(view);
    }

    private void passexam() {
        View view = LayoutInflater.from(this).inflate(R.layout.layout_refund_waitexam,null);
        sl.showCustomView(view);
    }

    private void waitexam() {
        View view = LayoutInflater.from(this).inflate(R.layout.layout_refund_successexam,null);
        sl.showCustomView(view);
    }
    private void wuliucommited(String resendCode,String resendCompany) {
        View view = LayoutInflater.from(this).inflate(R.layout.layout_refund_wuliucomited,null);
        TextView wuliunameTv=view.findViewById(R.id.tv_wuliuname);
        TextView wuliunoTv=view.findViewById(R.id.tv_wuliuno);
        wuliunameTv.setText(resendCompany);
        wuliunoTv.setText(resendCode);
        sl.showCustomView(view);
    }
    private void write(String returnAddress, String returnName, String returnTel) {
        View view = LayoutInflater.from(this).inflate(R.layout.layout_refund_writewuliu,null);
        TextView addressTv=view.findViewById(R.id.tv_address);
        addressTv.setText(returnAddress+""+returnName+""+returnTel);
        sl.showCustomView(view);
    }
}
